package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.union.UnionScuffleGift;
import com.motu.monstercity.server.game.commondata.union.UnionScufflePass;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class UnionScuffleBaseData {
    private static Logger logger = LoggerFactory.getLogger(UnionScuffleBaseData.class);

    public static void initBaseData(List<Future> futures) {
        futures.add(initUnionScuffleGift());
        futures.add(initUnionScufflePass());
    }
    private static ConcurrentHashMap<Integer, UnionScuffleGift> unionScuffleGiftMap = new ConcurrentHashMap<>(); // 联盟乱斗礼包表
    private static ConcurrentHashMap<Integer, List<UnionScuffleGift>> unionScuffleGiftGroupMap = new ConcurrentHashMap<>();

    /**
     * 初始化联盟乱斗礼包表
     */
    public static Future<Void> initUnionScuffleGift() {
        String table = "union_scuffle_gift";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, UnionScuffleGift> tmp = new ConcurrentHashMap<>();
                    ConcurrentHashMap<Integer, List<UnionScuffleGift>> tmpGroup = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        UnionScuffleGift entity = new UnionScuffleGift(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpGroup.computeIfAbsent(entity.getGroupId(), k -> new ArrayList<>()).add(entity);
                    }
                    unionScuffleGiftMap = tmp;
                    unionScuffleGiftGroupMap = tmpGroup;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static UnionScuffleGift getUnionScuffleGift(int id) {
        UnionScuffleGift result = unionScuffleGiftMap.get(id);
        if (result == null) {
            logger.error("getUnionScuffleGift error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, UnionScuffleGift> getUnionScuffleGiftMap() { return unionScuffleGiftMap; }

    public static List<UnionScuffleGift> getUnionScuffleGiftListByGroup(int groupId) {
        List<UnionScuffleGift> result = unionScuffleGiftGroupMap.get(groupId);
        if (result == null) {
            logger.error("getUnionScuffleGiftListByGroup error key = " + groupId);
            return new ArrayList<>();
        }
        return result;
    }

    public static UnionScuffleGift getUnionScuffleGiftByIapId(int iapGiftId) {
//        List<UnionScuffleGift> list = unionScuffleGiftGroupMap.get(actGroup);
//        if (list == null || list.isEmpty()) {
//            logger.error("getUnionScuffleGiftByIapId error groupKey = " + actGroup);
//            return null;
//        }
        UnionScuffleGift result = null;
        for (UnionScuffleGift gift : unionScuffleGiftMap.values()) {
            if (gift.getIapGiftId() == iapGiftId) {
                result = gift;
                break;
            }
        }
        return result;
    }
    private static ConcurrentHashMap<Integer, UnionScufflePass> unionScufflePassMap = new ConcurrentHashMap<>(); // 联盟乱斗通行证配置表
    private static List<UnionScufflePass> unionScufflePassList = new ArrayList<>();

    /**
     * 初始化联盟乱斗通行证配置表
     */
    public static Future<Void> initUnionScufflePass() {
        String table = "union_scuffle_pass";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + " order by id asc;";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, UnionScufflePass> tmp = new ConcurrentHashMap<>();
                    List<UnionScufflePass> tmpList = new ArrayList<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        UnionScufflePass entity = new UnionScufflePass(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        tmpList.add(entity);
                    }
                    unionScufflePassMap = tmp;
                    unionScufflePassList = tmpList;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static UnionScufflePass getUnionScufflePass(int id) {
        UnionScufflePass result = unionScufflePassMap.get(id);
        if (result == null) {
            logger.error("getUnionScufflePass error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, UnionScufflePass> getUnionScufflePassMap() { return unionScufflePassMap; }

    public static List<UnionScufflePass> getUnionScufflePassList() { return unionScufflePassList; }

    public static List<UnionScufflePass> getUnionScufflePassList(int start) {
        return unionScufflePassList.stream().filter(p -> p.getId() >= start).toList();
    }

}
